import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            component: Home
        },
        {
            path: '/home',
            component: Home
        },
        {
            path: '/login',
            component: Login
        }
    ]
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
    console.log("beforeEach")

    if(to.path === '/login') {
        next()
    } else {
        let username = window.localStorage.getItem('username')
        if(null != username) {
            next()
        } else{
            next('/login')
        }
    }
        
})

// 路由后置守卫
router.afterEach((to, from) => {
    console.log('afterEach');
})

export default router